package com.mic.yzzn.plc.common.ins.datastructure;

import java.io.Serializable;

/**
 *
 * Created by ASH on 16/4/11.
 */
public class PLCWordContacotNumber implements Serializable
{
    /** 起始字编码 */
    private String beginWordNumber = "0000";
    /** 结束字编码 */
    private String endWordNumber = "9999";

    public PLCWordContacotNumber()
    {

    }

    public PLCWordContacotNumber(String beginWordNumber, String endWordNumber)
    {
        setBeginWordNumber(beginWordNumber);
        setEndWordNumber(endWordNumber);
    }

    public String getBeginWordNumber()
    {
        return beginWordNumber;
    }

    public String getEndWordNumber()
    {
        return endWordNumber;
    }

    public void setBeginWordNumber(String beginWordNumber)
    {
        // 检测参数范围
        int beginValue = Integer.parseInt(beginWordNumber);
        int endValue = Integer.parseInt(this.endWordNumber);
        if (beginValue < 0 || beginValue > 9999)
        {
            throw new IllegalArgumentException("BCDCode is out of range(0 ~ 9999)");
        }

        if (beginValue > endValue)
        {
            throw new IllegalArgumentException("endBCDDataNumber must less than beginBCDDataNumber");
        }

        // 若不足四位则自动补足
        StringBuffer prefixBuffer = new StringBuffer();
        for (int i = beginWordNumber.length(); i < 4; ++i)
        {
            prefixBuffer.append("0");
        }
        prefixBuffer.append(beginWordNumber);

        this.beginWordNumber = prefixBuffer.toString();
    }

    public void setEndWordNumber(String endWordNumber)
    {
        // 检测参数范围
        int endValue = Integer.parseInt(endWordNumber);
        int beginValue = Integer.parseInt(this.beginWordNumber);
        if (endValue < 0 || endValue > 9999)
        {
            throw new IllegalArgumentException("BCDCode is out of range(0 ~ 9999)");
        }

        if (endValue < beginValue)
        {
            throw new IllegalArgumentException("endBCDDataNumber must less than beginBCDDataNumber");
        }

        // 若不足四位则自动补足
        StringBuffer prefixBuffer = new StringBuffer();
        for (int i = endWordNumber.length(); i < 4; ++i)
        {
            prefixBuffer.append("0");
        }
        prefixBuffer.append(endWordNumber);

        this.endWordNumber = prefixBuffer.toString();
    }

    @Override
    public String toString()
    {
        StringBuffer buffer = new StringBuffer();
        buffer.append(String.valueOf(this.beginWordNumber));
        buffer.append(this.endWordNumber);

        return buffer.toString();
    }

    /**
     * 计算自己每一位的异或结果并返回
     */
    public int getXORByBit()
    {
        int result = 0;
        for (int i = 0; i < beginWordNumber.length(); ++i)
        {
            result ^= beginWordNumber.charAt(i);
        }

        for (int i = 0; i < endWordNumber.length(); ++i)
        {
            result ^= endWordNumber.charAt(i);
        }

        return result;
    }

    /**
     * 返回该字编码的范围
     * @return 字编码的范围
     */
    public int range()
    {
        int endValue = Integer.parseInt(endWordNumber);
        int beginValue = Integer.parseInt(beginWordNumber);

        return endValue - beginValue + 1;
    }
}
